package com.benyanyi.picker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.benyanyi.picker.base.BaseDialogRecyclerAdapter;
import com.benyanyi.picker.bean.PickerBean;

import java.util.List;

/**
 * @author YanYi
 * @date 2020/6/22 16:22
 * @email ben@yanyi.red
 * @overview
 */
class PickerMsgAdapter extends BaseDialogRecyclerAdapter<PickerMsgAdapter.MsgHolder> {

    private Context context;
    private List<PickerBean<?>> list;

    private int selectPosition = -1;

    public PickerMsgAdapter(Context context, List<PickerBean<?>> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MsgHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_picker_msg, parent, false);
        return new MsgHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MsgHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bind(list.get(position).getName(), position == selectPosition);
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }


    public void notifyDataSetChanged(List<PickerBean<?>> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MsgHolder extends RecyclerView.ViewHolder {
        private TextView tv;
        private ImageView img;

        public MsgHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.item_picker_name);
            img = itemView.findViewById(R.id.item_picker_selectImg);
        }

        void bind(String name, boolean isSelect) {
            tv.setText(name);
            tv.setEnabled(isSelect);
            img.setVisibility(isSelect ? View.VISIBLE : View.GONE);
        }
    }
}
